package de.hm.edu.cep;

import java.util.ArrayList;
import java.util.UUID;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.client.UpdateListener;

import de.hm.edu.chartservice.ProductShare;
import de.hm.edu.chartservice.SalesVolume;


/**
 * Listener class for OrderEvents, that administrates ProductShare and SalesVolume and sends an KPIEvent
 * @author Peter Goldenbogen (Munich University of Applied Science)
 * @version 1.0 
 *
 */
public class OrderEventListener implements UpdateListener {
	
	@Override
	public void update(EventBean[] newEvents, EventBean[] oldEvents) {
		
		EventBean event = newEvents[0];
		String product = (String) event.get("product");
		int amount = (int) event.get("amount");
		
		ProductShare.getInstance().update(product, amount);
		SalesVolume.getInstance().update(amount);
		
		ArrayList<UUID> causality = new ArrayList<>();
		causality.add( (UUID) event.get("id"));
		
		KPIEvent kpiEvent = new KPIEvent(amount, causality);
		EventEngine.getEps().getEPRuntime().sendEvent(kpiEvent);
	}
}
